package com.hengyue.common.utils;
/**
 * 雪花算法ID生成器，使用了huTool工具
 */

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.id.IdentifierGenerator;

import java.io.Serializable;

public class SnowIdGenerator implements IdentifierGenerator {

    private final Snowflake snowflake = IdUtil.getSnowflake();

    public synchronized long snowflakeId() {
        return snowflake.nextId();
    }

    @Override
    public Serializable generate(SharedSessionContractImplementor session, Object object)
            throws HibernateException {
        return snowflakeId();
    }
}
